package com.comdo.newup_loan.module.recharge;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.comdo.newup_loan.BankWebViewActivity;
import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.http.ApiService;
import com.comdo.newup_loan.model.UserInfoBean;
import com.comdo.newup_loan.model.payTypeBean;
import com.comdo.newup_loan.module.home.ADWebviewActivity;
import com.comdo.newup_loan.util.PointLengthFilter;
import com.comdo.newup_loan.util.StringUtils;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.widget.MyEditText;
import com.comdo.newup_loan.widget.mySpinner.ComomData;
import com.comdo.newup_loan.widget.mySpinner.MySpinner;
import com.comdo.newup_loan.widget.mySpinner.onResultListener;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

//充值界面
public class MineTopUpActivity extends BaseActivity<RechargePersenter> implements OnClickListener, RechargeContact.View {

    // 账户可用余额,支付方式名字
    private TextView mTvAccount, mTvPayName, mTableText;
    // 充值金额
    private MyEditText mEtTopup;
    // 支付方式
    private LinearLayout mLlPaystyle;
    private Button mSubmit;
    private String flowNum;
    private MySpinner sp_types;
    private List<payTypeBean> types = new ArrayList<payTypeBean>();
    private int type;
    private String payType;
    private String amount;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_mine_topup;
    }

    @Override
    protected void init() {
        super.init();
        mPresenter.getUserInfo(Config.loginInfo.getUserId());
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTvAccount = (TextView) findViewById(R.id.tv_accountmoney2);
        mEtTopup = (MyEditText) findViewById(R.id.et_topup);
        mEtTopup.setFilters(new InputFilter[]{new PointLengthFilter()});
        mTvPayName = (TextView) findViewById(R.id.tv_payname);
        mLlPaystyle = (LinearLayout) findViewById(R.id.ll_paystyle);
        mLlPaystyle.setOnClickListener(this);
        mSubmit = (Button) findViewById(R.id.bt_submitcz);
        mSubmit.setOnClickListener(this);
        mTableText = (TextView) findViewById(R.id.tv_table);
        mTableText.setOnClickListener(this);
        if (Config.userInfo != null&&!StringUtils.isNull(Config.userInfo
                .getAvailable()))
        	mTvAccount.setText(StringUtils.getMoneyDC(Double.parseDouble(Config.userInfo
                    .getAvailable())) + "元");
        initToolBar();
    }

    @Override
    public void getFail(String failMessage) {
        mSubmit.setEnabled(true);
        showToast(failMessage);
    }

    @Override
    public void getflowNum(String flowNum) {
        mSubmit.setEnabled(true);
        Log.e("getflowNum", "getflowNum");

        mPresenter.getBankWeb(MineTopUpActivity.this, flowNum, amount, payType);
    }

    @Override
    public void getUserInfo(UserInfoBean info) {
        Config.userInfo = info;
        getTypes();
    }

    @Override
    public void getBankWeb(String url) {
        if (!StringUtils.isNull(url)) {
            toXwWeb(url);
        } else {
            showToast("page not found");
        }
    }

    private void initToolBar() {
        new TitleMenuUtil(MineTopUpActivity.this, "充值", false);
    }

    @Override
    public RechargePersenter getmPresenter() {
        return new RechargePersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    private void getTypes() {
        types.addAll(Config.userInfo.getPullInCompanyList());
        if(types==null&&types.size()==0)
            return;
        type = types.get(0).getId();
        payType = types.get(0).getValue();
        Drawable drawable = getResources().getDrawable(getPayIcon(type));
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mTvPayName.setCompoundDrawables(drawable, null, null, null);
        mTvPayName.setText(types.get(0).getName());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            // 确定
            case R.id.bt_submitcz:
                submit();
        /*	if (!BankUtils.hasBankRegisterDialog()) {
                submit();
			} else {
				DialogUtil.getToBankRegister(MineTopUpActivity.this);
			}
*/
                break;
            case R.id.ll_paystyle:
                if (sp_types == null) {
                    sp_types = new MySpinner(MineTopUpActivity.this, types, 1);
                    sp_types.setOnResultListener(new onResultListener() {
                        @Override
                        public void onResult(Object obj) {
                            if (obj != null) {
                                payType = ((payTypeBean) obj).getValue();
                                mTvPayName.setText(((ComomData) obj).getMyName());
                                type = ((ComomData) obj).getMyId();
                                Drawable drawable = getResources().getDrawable(getPayIcon(type));
                                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                                mTvPayName.setCompoundDrawables(drawable, null, null, null);
                            }
                        }
                    });
                } else {
                    sp_types.show();
                }
                break;
            case R.id.tv_table:
                if (!StringUtils.isNull(Config.userInfo.getAppLimitTable())) {
                    Intent intent = new Intent(MineTopUpActivity.this, ADWebviewActivity.class);
                    intent.putExtra("url", ApiService.PATHS+Config.userInfo.getAppLimitTable());
                    intent.putExtra("title", "限额表");
                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }

    private int getPayIcon(int type) {
        int drawable = R.drawable.zf_yb;
        switch (type) {
            case 0:
                drawable = R.drawable.zf_yb;
                break;
            case 1:
                drawable = R.drawable.zf_tl;
                break;
            case 2:
                drawable = R.drawable.zf_fy;
                break;
            case 3:
                drawable = R.drawable.zf_xs;
                break;
            case 4:
                drawable = R.drawable.zf_rb;
                break;
            default:
                drawable = R.drawable.zf_yb;
                break;
        }
        return drawable;
    }

    private void submit() {
        if (StringUtils.isNull(mEtTopup.getText().toString())) {
            Toast.makeText(getApplicationContext(), "请输入充值金额!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (mEtTopup.getText().toString().equals("0")) {
            Toast.makeText(getApplicationContext(), "充值金额不能为零!", Toast.LENGTH_SHORT).show();
            return;
        }
        mSubmit.setEnabled(false);
        amount = mEtTopup.getText().toString();
        mPresenter.getFlowNum(Config.loginInfo.getUserId(), amount);

    }

    private void toXwWeb(String url) {
        finish();
        Intent intent = new Intent(MineTopUpActivity.this, BankWebViewActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
    }

    @Override
    protected int setStatusBarColor() {
        return getResources().getColor(R.color.colorTheme);
    }

    @Override
    protected boolean translucentStatusBar() {
        return false;
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("充值");
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("充值");
        MobclickAgent.onPause(this);
    }
}
